﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using gt.dotnetcore.webapi.extensions;
using gt.dotnetcore.webapi.extensions.ApiException;
using gt.dotnetcore.webapi.extensions.ApiLogging;
using gt.ShortMessage.Services;
using gt.ShortMessage.Services.Impls;
using gt.ShortMessage.Services.Validators;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace gt.ShortMessage
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddMemoryCache();
            services.AddDataProtection(options => options.ApplicationDiscriminator = nameof(gt.ShortMessage));
            services.Configure<List<string>>(Configuration.GetSection("BlackPhoneList"));
            services.Configure<Dictionary<string, string>>(Configuration.GetSection("BusinessMessages"));
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterType<ActiveCodeService>().As<IActiveCodeService>().InstancePerDependency();
            builder.RegisterType<MessageProvider>().As<IMessageProivder>().SingleInstance();
            builder.RegisterType<ShortMessageService>().As<IShortMessageService>().InstancePerDependency();
            builder.RegisterType<EncryptTokenService>().As<ITokenService>().InstancePerDependency();

            builder.RegisterType<PhoneBlackListValidator>().Named<IPhoneValidator>("PHONE_BLACKLIST").SingleInstance();
            builder.RegisterType<PhonePerDayCountValidator>().Named<IPhoneValidator>("PHONE_PERDAYCOUNT").SingleInstance();
            builder.RegisterType<UniqueIdPerDayCountValidator>().Named<IUniqueIdValidator>("UNIQUEID_PERDAYCOUNT").SingleInstance();

            //可遍历类型注入，注意 只支持IEnumerable\IList\ICollection 类型
            builder.RegisterType<MessageSendValidator>().As<IMessageSendValidator>().SingleInstance();

            //builder.Register(c =>
            //{
            //    var phoneValidators = new List<IPhoneValidator>();
            //    var uniqueIdValidators = new List<IUniqueIdValidator>();

            //    phoneValidators.Add(c.ResolveNamed<IPhoneValidator>("PHONE_BLACKLIST"));
            //    phoneValidators.Add(c.ResolveNamed<IPhoneValidator>("PHONE_PERDAYCOUNT"));
            //    uniqueIdValidators.Add(c.ResolveNamed<IUniqueIdValidator>("UNIQUEID_PERDAYCOUNT"));
            //    var logger = c.Resolve<ILogger<MessageSendValidator>>();
            //    return new MessageSendValidator(phoneValidators, uniqueIdValidators, logger);
            //}).As<IMessageSendValidator>().SingleInstance();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseGlobalException();
            app.UseGlobalApiLogging();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
